/*
     raw os - Copyright (C)  Lingjun Chen(jorya_txj).

    This file is part of raw os.

    raw os is free software; you can redistribute it it under the terms of the 
    GNU General Public License as published by the Free Software Foundation; 
    either version 3 of the License, or  (at your option) any later version.

    raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
    without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program. if not, write email to jorya.txj@gmail.com
                                      ---

    A special exception to the LGPL can be applied should you wish to distribute
    a combined work that includes raw os, without being obliged to provide
    the source code for any proprietary components. See the file exception.txt
    for full details of how and when the exception can be applied.
*/


/* 	2012-8  Created by jorya_txj
  *	xxxxxx   please added here
  */


#include  <stdio.h>
#include  <string.h>
#include  <ctype.h>
#include  <stdlib.h>

#include "def.h"
#include "option.h"
#include "2440addr.h"     
#include "2440lib.h"
#include "2440slib.h"     
#include "mmu.h" 
#include "timer_bsp.h"

#include "raw_api.h"
#include "nand.h"


#define  NAND_TEST_STK_SIZE 2048

PORT_STACK nand_test_stack[NAND_TEST_STK_SIZE];
RAW_TASK_OBJ nand_test_obj;

extern void NF_Init(void);
void rNF_Reset(void);
char rNF_ReadID(void);

extern U8 rNF_ReadPage(U32 page_number);
U8 rNF_WritePage(U32 page_number,U8 *pbuf);
extern U8 rNF_EraseBlock(U32 block_number);
extern U8 rNF_RamdomRead(U32 page_number,U32 add);
extern U8 rNF_RamdomWrite(U32 page_number, U32 add, U8 dat);
extern U8 rNF_IsBadBlock(U32 block);
extern U8 rNF_MarkBadBlock(U32 block);
extern unsigned char buffer[2048];


void nand_test_task(void * pParam) 
{
	unsigned char Wbuf1[2048]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0x55};

	unsigned int j,k,m;
	unsigned char rtstat,rtstat_r,rtstat_w;

	Timer0Init();
	
	while(1) {

		NF_Init();

		rNF_Reset();
		for ( k = 2; k < 2048; k++ )
		{
			rtstat = rNF_EraseBlock(k);
			for ( m = 0; m < 64; m++ )
			{
				rtstat_w = rNF_WritePage( (k-1)*64+m, Wbuf1);
				if(rtstat_w != 0x66)Uart_Printf("write fail ...\n");
				rtstat_r = rNF_ReadPage( (k-1)*64+m );
				if(rtstat_r != 0x66)Uart_Printf("read fail ...\n");
				Uart_SendByte('.');
			}
		}
		Uart_Printf("test over ...\n");
		
		rtstat = rNF_EraseBlock(1);
		Uart_Printf("rNF_EraseBlock return : %d\n",rtstat);

		rtstat = rNF_WritePage(1*64,Wbuf1);
		Uart_Printf("rNF_WritePage return : %d\n",rtstat);

		rtstat = rNF_ReadPage(1*64);
		Uart_Printf("rNF_ReadPage return : %d\n",rtstat);
		for(j = 0; j < 64; j++)Uart_Printf("%x ", buffer[j]);
		Uart_Printf("\n");

		rtstat = rNF_RamdomWrite(10*64+1,0,0x39);
		Uart_Printf("rNF_RamdomWrite return : %d\n",rtstat);
		rtstat = rNF_RamdomRead(10*64+1,0);
		Uart_Printf("rNF_RamdomRead = %x\n", rtstat);
		for(j = 0; j < 64; j++)Uart_Printf("%x ", buffer[j]);
		Uart_Printf("\n");
		
		rNF_Reset();
		Uart_Printf("rNF_ReadID : %d\n", rNF_ReadID());
		while(1);
		
	}
	
}


extern RAW_U8 test_started;
void nand_test(void)
{

	if (test_started) {

		return;

	}

	test_started = 1;
	
	raw_task_create(&nand_test_obj, (RAW_U8  *)"nand_test", 0,
	                         10, 0,  nand_test_stack, 
	                         NAND_TEST_STK_SIZE ,  nand_test_task, 1); 	
	

}


